package com.example.springboot.service.impl;

import com.example.springboot.mapper.NoticeMapper;
import com.example.springboot.pojo.Notice;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.User;
import com.example.springboot.service.NoticeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.example.springboot.util.TimeUtil.getNow;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public Notice findById(Integer id) {
        return noticeMapper.findById(id);
    }

    @Override
    public void addNotice(String tittle, String content, String type, Integer status) {
        noticeMapper.addNotice(tittle,content,type,status,getNow());
    }

    @Override
    public void publish(Integer id) {
        noticeMapper.publish(id,getNow());
    }

    @Override
    public PageBean<Notice> getList(Integer pageNum, Integer pageSize) {
        PageBean<Notice> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<Notice> notices = noticeMapper.getList();
        Page<Notice> p = (Page<Notice>) notices;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void deleteNotice(Integer id) {
        noticeMapper.deleteNotice(id);
    }

    @Override
    public void updateNotice(Integer id, String tittle, String type, String content) {
        noticeMapper.updateNotice(id,tittle,type,content,getNow());
    }

    @Override
    public PageBean<Notice> getByType(String type,Integer pageNum, Integer pageSize) {
        PageBean<Notice> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<Notice> notices = noticeMapper.getByType(type);
        Page<Notice> p = (Page<Notice>) notices;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
//        return noticeMapper.getByType(type);
    }
}
